package cn.siat.gene.controller;

import cn.siat.common.core.domain.R;
import cn.siat.common.core.web.controller.BaseController;
import cn.siat.common.core.web.domain.AjaxResult;
import cn.siat.gene.service.AssociatedMoleculeService;
import cn.siat.gene.service.DesignService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/molecular")
@RequiredArgsConstructor
@Slf4j
public class MolecularController extends BaseController {
    private final AssociatedMoleculeService associatedMoleculeService;
    private final DesignService designService;

    /**
     * 关联分子方案-gRNA
     * @return
     */
    @PostMapping("/grna")
    public AjaxResult sendGRNA(@RequestParam("configureFile") MultipartFile configure,
                               @RequestParam("vectorFile") MultipartFile vector,
                               @RequestParam(value="genomeFile",required=false ) MultipartFile genome,
                               @RequestParam("params") String params){
        String gRNA = associatedMoleculeService.gRNA(configure, vector, genome, params);
        return AjaxResult.success("成功",gRNA);
    }

    /**
     * 关联分子方案-序列组装
     * @param configure
     * @param vector
     * @param genome
     * @param params
     * @return
     */
    @PostMapping("/primerPrediction")
    public AjaxResult primerPrediction(@RequestParam("configureFile") MultipartFile configure,
                                       @RequestParam("vectorFile") MultipartFile vector,
                                       @RequestParam(value="genomeFile",required=false ) MultipartFile genome,
                                       @RequestParam("params") String params){
        String primerPrediction = associatedMoleculeService.primerPrediction(configure, vector, genome, params);
        return AjaxResult.success("成功",primerPrediction);
    }
    /**
     * 关联分子方案-基因敲除
     * @param configure
     * @param vector
     * @param genome
     * @param params
     * @return
     */
    @PostMapping("/geneKnockout")
    public AjaxResult geneKnockout(@RequestParam("configureFile") MultipartFile configure,
                                   @RequestParam("vectorFile") MultipartFile vector,
                                   @RequestParam("kanMXFile") MultipartFile kanMX,
                                   @RequestParam(value="genomeFile",required=false ) MultipartFile genome,
                                   @RequestParam("params") String params){
        String primerPrediction = associatedMoleculeService.geneKnockout(configure, vector, kanMX,genome, params);
        return AjaxResult.success("成功",primerPrediction);
    }

    /**
     * 关联分子方案-其它
     * @return
     */
    @PostMapping("/other")
    public AjaxResult other(@RequestParam("configureFile") MultipartFile configure,
                            @RequestParam("params") String params){
        String other = associatedMoleculeService.other(configure, params);
        return AjaxResult.success("成功",other);
    }

    /**
     * 下载关联分子方案结果
     * @param programmeId
     * @param step
     * @return
     * @throws IOException
     */
    @GetMapping(value = "/resultDownload")
    public ResponseEntity<?> resultDownload(String programmeId, String step) throws IOException {
        return associatedMoleculeService.resultDownload(programmeId,step);
    }

    @GetMapping(value = "/sampleFile")
    public ResponseEntity<?> sampleFile(String fileName) throws IOException {
        return associatedMoleculeService.sampleFile(fileName);
    }

    /**
     * 检查是否运行完毕
     * @param programmeId
     * @param step
     * @return
     */
    @GetMapping(value = "/checkIsCompletes")
    public AjaxResult checkIsCompletes(String programmeId, String step){
        return AjaxResult.success("成功",associatedMoleculeService.checkIsCompletes(programmeId,step));
    }

    /**
     * 回滚时删除失效关联分子方案文件
     * @param params
     * @return
     */
    @DeleteMapping(value = "/delStepFiles")
    public AjaxResult delStepFiles(@RequestBody Map<String,Object> params){
        return AjaxResult.success("成功",associatedMoleculeService.delStepFiles(params));
    }

    @PostMapping(value = "/rollBack")
    public AjaxResult rollBack(String programmeId, String step , String operateId , String mode){
        associatedMoleculeService.rollBack(programmeId,step,operateId,mode);
        return AjaxResult.success();
    }

    /**
     * 获取装置设计token
     * @return
     */
    @GetMapping("/getDesignToken")
    public AjaxResult getDesignToken(){
        R<Map> r = designService.getDesignToken();
        return new AjaxResult(r.getCode(),r.getMsg(),r.getData());
    }

    /**
     * 新建装置设计项目
     * @param map
     * @return
     */
    @PostMapping("/insertProject")
    public AjaxResult insertProject(@RequestBody Map<String,String> map){
        R<String> r = designService.insertDesignProject(map);
        return new AjaxResult(r.getCode(),r.getMsg(),r.getData());
    }

    /**
     * 新建装置设计设计
     * @param map
     * @return
     */
    @PostMapping("/insertDesign")
    public AjaxResult insertDesign(@RequestBody Map<String,String> map){
        R<String> r = designService.insertDesign(map);
        return new AjaxResult(r.getCode(),r.getMsg(),r.getData());
    }

    /**
     * 获取下载文件URL
     * @param designId
     * @return
     */
    @GetMapping("getDesignFile")
    public AjaxResult getDesignFile(String designId){
        String fileUrl = designService.getDesignFile(designId);
        return AjaxResult.success("成功!",fileUrl);
    }

    /**
     * 装置设计撤回
     * @param stepId
     * @param designId
     * @return
     */
    @DeleteMapping("designRevocation")
    public AjaxResult designRevocation(String stepId,String designId){
        designService.designRevocation(stepId,designId);
        return AjaxResult.success("撤回成功!");
    }
}
